capture log close
clear all
set more off
pause off
# delimit;

local output "";
local log "";
local data "";

/* 
Create Figure 1B 
*/

use "`data'\collapsemact_combine.dta", clear;


/* ******************************* */
/* **** DATA PREP AND RESHAPE **** */
/* ******************************* */

/* Create month-year and age variables */
gen ser_dobym=ym(ser_dobyy, ser_dobmm);
gen yearmo=ym(year, month);
gen age_month=yearmo-ser_dobym;
replace age_month=9999 if year==9999;

/* Collapse by month-year cohort and age_month */
collapse (sum) earn10yr* earn55*, by(ser_dobym ser_dobyy ser_dobmm age_month);

/* Create fractions of cohorts instead of counts (for flow variable, use single event denominator to avoid double counting people) */
foreach set in earn10yr earn55 {;
foreach stub in `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany {;
	egen total_`stub'=total(`stub'), by(ser_dobym);
};
};
foreach set in earn10yr earn55 {;
foreach stub in `set'_claim_phusold `set'_claim_phusany `set'_claim_mbaold `set'_claim_mbaany {;	
	rename `stub' ct_`stub';
	gen `stub'=ct_`stub'/total_`stub' if ct_`stub'>10;
	replace `stub'=. if ct_`stub'<=10;
};
};
drop total_* ct_*;

/* Drop retire/claim ages that are too young to be plausible */
drop if age_month<50*12;

/* Collapse to group cohorts (YOB) together */
collapse 
	earn10yr_claim_phusold earn10yr_claim_phusany earn10yr_claim_mbaold earn10yr_claim_mbaany
	earn55_claim_phusold earn55_claim_phusany earn55_claim_mbaold earn55_claim_mbaany
	, by(age_month ser_dobyy);

/* Reshape for plot */
reshape wide 
	earn10yr_claim_phusold earn10yr_claim_phusany earn10yr_claim_mbaold earn10yr_claim_mbaany
	earn55_claim_phusold earn55_claim_phusany earn55_claim_mbaold earn55_claim_mbaany
, i(age_month) j(ser_dobyy);


/* ***************************************************** */
/* **** PLOT RETIREMENT AND CLAIMING ACROSS COHORTS **** */
/* ***************************************************** */

local color_claim_mbaany "green";

gen age=age_month/12;

foreach set in earn10yr {;
foreach stub in claim_phusany {;
twoway 	
	connected `set'_`stub'1938 age if age>=61.5 & age<66.5, lcolor(`color_`stub''*2) lwidth(medthick) lpattern(solid) mcolor(`color_`stub''*2) msymbol(T) ||
	connected `set'_`stub'1940 age if age>=61.5 & age<66.5, lcolor(`color_`stub''*1.5) lwidth(medthick) lpattern(longdash) mcolor(`color_`stub''*1.5) msymbol(O) ||
	connected `set'_`stub'1942 age if age>=61.5 & age<66.5, lcolor(`color_`stub''*1) lwidth(medthick) lpattern(shortdash) mcolor(`color_`stub''*1) msymbol(D) ||
	connected `set'_`stub'1944 age if age>=61.5 & age<66.5, lcolor(`color_`stub''*.75) lwidth(medthick) lpattern(shortdash) mcolor(`color_`stub''*.75) msymbol(S) ||
	connected `set'_`stub'1946 age if age>=61.5 & age<66.5, lcolor(`color_`stub''*.5) lwidth(medthick) lpattern(longdash) mcolor(`color_`stub''*.5) msymbol(Th) ||
	connected `set'_`stub'1948 age if age>=61.5 & age<66.5, lcolor(`color_`stub''*.25) lwidth(medthick) lpattern(solid) mcolor(`color_`stub''*.25) msymbol(Oh)
	xtitle("Age", size(medsmall)) xsc(r(61 67)) xlab(61(1)67)
	ytitle("Fraction of birth cohort", size(medsmall))
	legend(col(3) row(2) label(1 "1938") label(2 "1940") label(3 "1942") label(4 "1944") label(5 "1946") label(6 "1948"))
	graphregion(color(white))
	bgcolor(white)
	ylabel(,nogrid)
	;
	graph export "`output'\\Figure_1B.png", replace;
};
};